#-*- coding: utf-8 -*-

import urllib2 as urllib
import re
import cookielib
from bs4 import BeautifulSoup


def getHtml(url):
    cj=cookielib.CookieJar()
    opener=urllib.build_opener(urllib.HTTPCookieProcessor(cj))
    opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'),('Cookie','4564564564564564565646540')]

    urllib.install_opener(opener)
    
    html_bytes = urllib.urlopen( url ).read()
    html_string = html_bytes.decode( 'utf-8' )
    return html_string

# #url = http://zst.aicai.com/ssq/openInfo/
# #最终输出结果格式如：2015075期开奖号码：6,11,13,19,21,32, 蓝球：4
# html = getHtml("http://zst.aicai.com/ssq/openInfo/")

# #print(repr(html))

# #soup = BeautifulSoup(html, 'html.parser')
# soup = BeautifulSoup(html, 'lxml')

# # print(soup.prettify())
# # a = soup.prettify()

# # print(repr(a))
# a = soup.find_all('table')



# # print(repr(soup.find_all('table')))

# # print(a)
# # print('\n\n')
# # print(type(a))
# # print('\n\n')
# # print(type(a[0]))
# # print('\n\n\n\n')
# # print(len(a))

# b = a[0].find_all('tr')
# # print('\n\n')
# # print(len(b))

# print(b[0])
# print('\n\n')
# print(b[1])
# print('\n\n')
# print(b[2])
# print('\n\n')
# print(b[3])

# latest = b[2].find_all('td')

# period = latest[0].string

# date = latest[1].string

# balls = ''

# for i in range(2,9):
#     balls += latest[i].string
#     balls += ','

# print(period)

# print(date)

# print(balls)



def get_latest():
    html = getHtml("http://zst.aicai.com/ssq/openInfo/")

    soup = BeautifulSoup(html, 'lxml')

    a = soup.find_all('table')

    b = a[0].find_all('tr')

    latest = b[2].find_all('td')


    record = ''

    period = latest[0].string

    record += period



    date = latest[1].string

    record += ','

    record += date 

    balls = ''

    for i in range(2,9):
        record += ','
        record += latest[i].string
        
        balls += latest[i].string
        balls += ','

    print(period)
    print(type(period))
    print(repr(period))
    

    print(date)
    print(type(date))
    print(repr(date))


    print(balls)
    print(type(balls))
    print(repr(balls))

    print(record)
    print(type(record))
    print(repr(record))

    return record




a = get_latest()

print('-----------------')
print(a)








#<table class="fzTab nbt"> </table>
# print('\n\n\n\n\n-----------------------html-------------------------------------------------------\n')
# print(html)
# table = html[html.find('<table class="fzTab nbt">') : html.find('</table>')]
# # print('\n\n\n\n\n-------------------------table-----------------------------------------------------\n')
# # print(repr(table))
# #<tr onmouseout="this.style.background=''" onmouseover="this.style.background='#fff7d8'">
# #<tr \r\n\t\t                  onmouseout=
# tmp = table.split('<tr \r\n\t\t                  onmouseout=')
# # print('\n\n\n\n\n---------------------------tmp---------------------------------------------------\n')
# # #print(tmp)
# # print(len(tmp))
# # print('\n\n')
# #for i in tmp:
# #   print(repr(i))
# for i in range(len(tmp)):
#     trs = tmp[i]
#     tr = trs[: trs.find('</tr>')]
#     # print(tr)
#     number = tr.split('<th')[1].split('</th>')[0]
#     print(number + u'期开奖号码：')
#     # redtmp = tr.split('<th  class="redColor sz12" >')
#     # reds = redtmp[1:len(redtmp)-1]#去掉第一个和最后一个没用的元素
#     # #print(reds)
#     # for redstr in reds:
#     #         print(redstr.split('</th>')[0] + ",")
#     # print(u'蓝球：')
#     # blue = tr.split('<th  class="blueColor sz12" >')[1].split('</th>')[0]
#     # print(blue)
